package com.ruoyi.kanban.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.kanban.domain.vo.*;
import com.ruoyi.kanban.service.IKanbanDashboardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/kanban/dashboard")
public class KanbanDashboardController extends BaseController {

    @Autowired
    private IKanbanDashboardService dashboardService;

    // 概览
    @PreAuthorize("@ss.hasPermi('kanban:dashboard:summary')")
    @GetMapping("/receiver/summary")
    public AjaxResult getReceiverSummary(@RequestParam(value = "startTime", required = false) Date startTime,
                                         @RequestParam(value = "endTime", required = false) Date endTime) {
        DashboardSummaryVO vo = dashboardService.getReceiverSummary(startTime, endTime);
        return AjaxResult.success(vo);
    }

    // 趋势
    @PreAuthorize("@ss.hasPermi('kanban:dashboard:trend')")
    @GetMapping("/receiver/trend")
    public AjaxResult getReceiverTrend(@RequestParam(value = "trendGranularity", required = false, defaultValue = "day") String trendGranularity,
                                       @RequestParam(value = "startTime", required = false) Date startTime,
                                       @RequestParam(value = "endTime", required = false) Date endTime) {
        List<DashboardTrendItemVO> list = dashboardService.getReceiverTrend(trendGranularity, startTime, endTime);
        return AjaxResult.success(list);
    }

    // 分布
    @PreAuthorize("@ss.hasPermi('kanban:dashboard:distribution')")
    @GetMapping("/receiver/distribution")
    public AjaxResult getReceiverDistribution(@RequestParam(value = "startTime", required = false) Date startTime,
                                              @RequestParam(value = "endTime", required = false) Date endTime) {
        List<DashboardDistributionItemVO> list = dashboardService.getReceiverDistribution(startTime, endTime);
        return AjaxResult.success(list);
    }

    // 实时列表（分页）
    @PreAuthorize("@ss.hasPermi('kanban:dashboard:realtime')")
    @GetMapping("/receiver/realtime")
    public TableDataInfo getReceiverRealtime(@RequestParam(value = "startTime", required = false) Date startTime,
                                             @RequestParam(value = "endTime", required = false) Date endTime) {
        startPage();
        List<DashboardRealtimeItemVO> list = dashboardService.getReceiverRealtime(startTime, endTime);
        return getDataTable(list);
    }

    // 异常统计
    @PreAuthorize("@ss.hasPermi('kanban:dashboard:exceptions')")
    @GetMapping("/receiver/exceptions")
    public AjaxResult getReceiverExceptions(@RequestParam(value = "startTime", required = false) Date startTime,
                                            @RequestParam(value = "endTime", required = false) Date endTime) {
        List<DashboardExceptionItemVO> list = dashboardService.getReceiverExceptions(startTime, endTime);
        return AjaxResult.success(list);
    }
}